var ns, app;
app = new Vue({
    el: '#app',
    data: {
        list: [], 
        form: {
            keywords: '',//关键词
            pageindex: 0,
            pagesize: 10,
            status: 0, //0 全部 1-待接单 2-处理中 3-已完工 4-已关单
            RoomID: 0,
            isviewhistory: true,
            startTime: '',//开始时间
            endTime: '',//结束时间
            activeDate: 0,            
            activeDateDesc: '时间',
            ProjectStatusIDs: [],//筛选状态数组
        },
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearch:false,
        showsearchmonth: false,
        showsearchparam: false,
        projectList:[
            {"ID":1,"Name":"待接单"},
            {"ID":2,"Name":"整改中"},
            {"ID":3,"Name":"验收中"},
            {"ID":4,"Name":"已关单"},
        ],
        IsHistory:1,//0 列表 1 历史工单
    },
    methods: {
        get_data: function() {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.IsHistory = that.IsHistory;
            options.RoomID = that.form.RoomID;
            options.status = that.form.status;
            options.keywords = that.form.keywords;//关键词
            options.startTime = that.form.startTime;//开始时间
            options.endTime = that.form.endTime;//结束时间
            options.statusList = JSON.stringify(that.form.ProjectStatusIDs);//状态筛选，支持多选和单选
            options.action = "getqualitycontrollist";
            options.isAnalysis = that.form.isviewhistory ? 1 : 0;

            ns.post(options, function(succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    that.list = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        openGongdan: function(item) {
            var that = this;
            ns.openWin('qualitycontroldetail_frm', '工单详情', {
                id: item.ID,
                isviewhistory: that.isviewhistory
            });
        },
        doSelectSearchParam: function(type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function() {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentWeek = dateRangeUtil.getCurrentWeek();
                startDate = dateRangeUtil.getDateString(currentWeek[0]);
                endDate = dateRangeUtil.getDateString(currentWeek[1]);
                that.form.activeDateDesc = '本周';
            }
            if (dataValue == 3) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
           
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        do_select_time: function(type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function(ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        doSearch:function(){
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
        doReset:function(type){
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc = '时间';
            } else {
                that.form.ProjectStatusIDs = [];
            }
        },
        
        doSelectProject: function(ID) {
            var that = this;
            that.form.status = ID;
            var index = that.form.ProjectStatusIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectStatusIDs.splice(index, 1);
            } else {
                that.form.ProjectStatusIDs.push(ID);
            }
        },
        do_search: function() {
            this.get_data();
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function(succeed, data, err) {
                if(data.status == true && data.ServiceStatus == item.ServiceStatus){
                    that.do_canaccpet(item);
                }else{
                    api.toast({
                        msg: "状态已改变，请重新选择",
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });

                }
                
            })
        },
        do_canaccpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptorderquality";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    else {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.form.status = 2;
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //整改完成
        do_zgcomplete:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_zhenggai', '整改完成', {
                        ID:item.ID,
                        ProjectID: item.ProjectID,
                        CheckUserID:item.CheckUserID,
                        CheckUserName:item.CheckUserName,
                        type:1,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });
            
        },
        //验收
        do_yanshou:function(item){
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_yanshou', '整改验收', {
                        ID:item.ID,
                        ProjectID: item.ProjectID,
                        CheckUserID:item.CheckUserID,
                        CheckUserName:item.CheckUserName,
                        type:2,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });

        },
        //催单
        do_open_cuidan: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "qualitycuidan";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    else {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //处理
        do_chuli: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_frm', '录入处理记录', {
                        id: item.ID,
                        type: 1,
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                }
            });
        },
        //派单 重派
        do_open_chooseuser: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '选择负责人', {
                        source: 'chooseuser',
                        id: item.ID,
                        type: 1,
                        ServiceType: 3
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                }
            });
        },
        //完工
        do_complete: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_frm', '录入完工记录', {
                        id: item.ID,
                        type: 2,
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                }
            });

        },
        //关单
        do_open_dialog: function (item) {
            var that = this;
            api.confirm({
                title: "提示",
                msg: '工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons: ['确认关闭', '取消']
            }, function (ret) {
                if (ret.buttonIndex == 1) {
                    var options = {};
                    options.ID = item.ID;
                    options.action = "closequality";
                    ns.post(options, function (succeed, data, err) {
                        if (succeed) {
                            if (data.status) {
                                api.toast({
                                    msg: "操作成功",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                            else {
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                            that.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });
                }
            })
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                     //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },

    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.RoomID = api.pageParam.id || 0;
    app.form.isviewhistory = api.pageParam.isviewhistory || false;
    //if (app.form.isviewhistory) {
    //    app.form.status = 4;
    //}
    //app.doClickDay(1);
    ns.init_pull(true);
    ns.init_search();
    app.get_data();

    api.addEventListener({
        name: 'reloadservicelist'
    }, function (ret, err) {
        app.get_data();
    });

};
